<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  qr-decomposition.html
  QR decomposition demo
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>QR decomposition</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>QR decomposition</h1>
        <form>
            <div>
                Let's compute a QR factorization of a 3x3 matrix.
                <br>
                Simply edit the fields below.
            </div>
            <div class="separator"></div>
            <div>
                <table>
                    <tr>
                        <td><input type="number" id="a0" step="0.5" value="1.0"></td>
                        <td><input type="number" id="a3" step="0.5" value="0.0"></td>
                        <td><input type="number" id="a6" step="0.5" value="0.0"></td>
                    </tr>
                    <tr>
                        <td><input type="number" id="a1" step="0.5" value="0.0"></td>
                        <td><input type="number" id="a4" step="0.5" value="1.0"></td>
                        <td><input type="number" id="a7" step="0.5" value="0.0"></td>
                    </tr>
                    <tr>
                        <td><input type="number" id="a2" step="0.5" value="0.0"></td>
                        <td><input type="number" id="a5" step="0.5" value="0.0"></td>
                        <td><input type="number" id="a8" step="0.5" value="1.0"></td>
                    </tr>
                </table>
            </div>
            <div class="separator"></div>
        </form>
        <pre id="output">Edit any field to begin!</pre>
        <script>
            window.onload = function() {
                const output = document.querySelector('#output');
                const input = {
                    a: [ ...(new Array(9).keys()) ].map(j => document.querySelector('#a' + j))
                };

                // setup event listeners
                input.a.forEach(field =>
                    field.addEventListener('input', qr)
                );

                // output
                function print(x)
                {
                    output.innerText += x.toString() + '\n';
                }

                function clear()
                {
                    output.innerText = '';
                }

                // compute and print QR
                async function qr()
                {
                    // read the matrix from the input fields
                    const A = Speedy.Matrix(3, 3, input.a.map(field => Number(field.value)));

                    // compute QR
                    const Q = Speedy.Matrix.Zeros(3, 3);
                    const R = Speedy.Matrix.Zeros(3, 3);
                    await Speedy.Matrix.qr(Q, R, A);
                    
                    // print the factorization
                    clear();
                    print('Matrix Q:\n');
                    print(Q);
                    print('\nMatrix R:\n');
                    print(R);

                    // validate
                    const QR = Speedy.Matrix.Zeros(3, 3);
                    await QR.setTo(Q.times(R));
                    print('\nMatrix QR:\n');
                    print(QR);
                }
            }
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>